Skip to content

Removed additional forward slash causing 404 error #49496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 23, 2025

Conversation

james-gould
Copy link
Contributor

@james-gould james-gould commented Apr 20, 2025

The additional / in both restore API calls results in the URL being:

  • https://{vaultName}.vault.azure.net/certificates//restore
  • https://{vaultName}.vault.azure.net/keys//restore

Removing the additional / constructs a correct path for the request.

For context: I'm building the Azure Key Vault Emulator and have hit this in both keys and certificates, the SecretsClient has the correct path and works without a problem. I raised this a few days in my repo when I found the bug in the KeysClient, having just repeated it in the CertificatesClient I've dug further and got it reproducing. Emulator issue here: james-gould/azure-keyvault-emulator#89

Using client library version(s) 4.7.0 this middleware is currently necessary to workaround the bug:

public class RestoreDoubleSlashRerouteMiddleware(RequestDelegate next)
{
    public async Task InvokeAsync(HttpContext context)
    {
        var originalPath = context.Request.Path.Value;

        if (originalPath is not null && originalPath.Contains("//"))
        {
            var rerouted = Regex.Replace(originalPath, "/{2,}", "/");

            context.Request.Path = new PathString(rerouted);
        }

        await next(context);
    }
}

I'd guess that something similar exists in the backend of Key Vault too.

@github-actions github-actions bot added Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. KeyVault labels Apr 20, 2025
Copy link

Thank you for your contribution @james-gould! We will review the pull request and get back to you soon.

@james-gould
Copy link
Contributor Author

@microsoft-github-policy-service agree

@azure-sdk
Copy link
Collaborator

API change check

API changes are not detected in this pull request.

@james-gould
Copy link
Contributor Author

Build failures are due to the request and recorded URL being different (which is the change), please advise on next steps?

@jsquire
Copy link
Member

jsquire commented Apr 20, 2025

Hi @james-gould. Thank you for your contribution and interest in improving the Azure developer experience. The test failures do look to be related to test recordings and would require re-recording. Steps for doing so can be found in the recording section of the test framework README.

@jsquire
Copy link
Member

jsquire commented Apr 20, 2025

@jorgerangel-msft: I'm going to kick the live tests. Can you please take a look at the results and advise whether there's a gap in live testing coverage or in the scenario that is causing a 404 for @james-gould?

@jsquire
Copy link
Member

jsquire commented Apr 20, 2025

/azp run net - keyvault - tests

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@james-gould
Copy link
Contributor Author

Sure, will wait for the tests to run. Thanks for getting back so quickly!

@james-gould
Copy link
Contributor Author

Will take a little time to record the tests again, need to set everything up locally. Will update the PR when it's ready!

@james-gould
Copy link
Contributor Author

I'm at a complete loss with this now, I've spent ~7 hours getting the test-proxy working, recording (not) working etc. Towards the end of last night I suddenly started getting 403 errors, despite the RBAC Key Vault Administrator role on a manually created and script created (via New-TestResources.ps1) vaults. I've created an entirely new Azure account + subscription, configured entra from scratch and still getting the same.

I'm gonna spend another 30-35 minutes trying to get these recordings sorted but I really can't spend much more time banging my head against the Azure wall trying to get all of this running unfortunately.

@james-gould
Copy link
Contributor Author

james-gould commented Apr 21, 2025

2 hours later still the same. Purged everything (again) and went back to scratch.

Set the values (removed for safety below, they're filled in locally) for these and wrote them into PS7:

${env:KEYVAULT_TENANT_ID} = ''
${env:KEYVAULT_CLIENT_ID} = ''
${env:KEYVAULT_CLIENT_SECRET} = ''
${env:KEYVAULT_SUBSCRIPTION_ID} = ''
${env:KEYVAULT_RESOURCE_GROUP} = ''
${env:KEYVAULT_LOCATION} = ''
${env:KEYVAULT_SKU} = ''
${env:AZURE_KEYVAULT_URL} = ''

Ran:

.\New-TestResources.ps1 -BaseName 'sdkresources' -ServiceDirectory 'keyvault' -SubscriptionId '<id>' -ResourceGroupName 'rg-sdkresources' -Location 'uksouth'

Which created the resources correctly:

imgur

Checking IAM for the vault:

Verified the .env is available in the keyvault directory, then ran:

setx KEYVAULT_TENANT_ID ${env:KEYVAULT_TENANT_ID}
setx KEYVAULT_CLIENT_ID ${env:KEYVAULT_CLIENT_ID}
setx KEYVAULT_CLIENT_SECRET ${env:KEYVAULT_CLIENT_SECRET}
setx KEYVAULT_SUBSCRIPTION_ID ${env:KEYVAULT_SUBSCRIPTION_ID}
setx KEYVAULT_RESOURCE_GROUP ${env:KEYVAULT_RESOURCE_GROUP}
setx KEYVAULT_LOCATION ${env:KEYVAULT_LOCATION}
setx KEYVAULT_SKU ${env:KEYVAULT_SKU}
setx AZURE_KEYVAULT_URL ${env:AZURE_KEYVAULT_URL}

Verified the values were set and persisted, then opened visual studio to run the tests. Ran a single one and got the prompt:

The resources needed to run the live tests could not be located.
Would you like to run the resource creation script? [y/n]:

Inputting Y then runs the New-TestResources.ps1 script but creates the resource group rg-jameskeyvault and then fails with the error in activity log:

[
  {
    "code": "StorageAccountAlreadyTaken",
    "target": "t5a619685e07b15a6prim",
    "message": "The storage account named t5a619685e07b15a6prim is already taken."
  }
]

but the resource group is empty. Inputting "n" just immediately fails the test with a 403.

I can't spend anymore time on this, I fix an error in one area and something else crops up. These resource scaffolding errors only started happening this morning, prior to that it was the 403 with:

    Azure.RequestFailedException : Caller is not authorized to perform action on resource.

I'm assuming that's related to this section, ie the credential isn't working but the .env isn't being used in the root of the keyvault directory.

@jsquire If you (or someone else) can help to resolve the config problems with me I'm happy to rerecord the tests and update the PR but if not I'll mark it as abandoned and put a workaround in the emulator.

@JonathanCrd
Copy link
Member

Hi @james-gould, I'm looking at your issues with the recorder, my guess here is that a Key Vault HSM is required for some tests and that is why you are getting prompted with "The resources needed to run the live tests could not be located." message.

For that, you'll have to run a script command like this:

.\New-TestResources.ps1 -ServiceDirectory keyvault -AdditionalParameters @{enableHsm=$true; hsmLocation='uksouth'} -Location 'uksouth' -ResourceGroupName 'rg-sdkresources' -BaseName 'sdkresources' -UserAuth -SubscriptionId '<id>'

I'm going to try to replicate your issue in a new environment, I'll let you know what I find.

@james-gould
Copy link
Contributor Author

Thanks @JonathanCrd that makes sense, looking forward to see if it's repeatable your end. Assuming all is well, is there a way to record all tests rather than capturing a specific one with the x-recording-file key?

@JonathanCrd
Copy link
Member

@james-gould When you run the tests, by default they will run in Playback mode, and if a test fails the test proxy tool will attempt to re-record it. It would look something like this.
image

If you want to record all the tests, you can do so by setting the Test Mote to Record and then run all of them, but in theory you wouldn't need to do that.

I was able to re-record after deploying the test resources with a managed HSM, so that was likely the problem. I'm going to push a commit to this PR to update the recordings.

Thank you again for your contribution

Copy link
Member

@JonathanCrd JonathanCrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@james-gould
Copy link
Contributor Author

When you run the tests, by default they will run in Playback mode, and if a test fails the test proxy tool will attempt to re-record it. It would look something like this.

Annoying, I’d set it to record mode (uncommenting the enum in the base classes for the 2 clients) but couldn’t get the actual tests to proxy through. Using an API tool to speak to the proxy was fine.

Appreciate the info, I’ll rerun it my end tomorrow just to see it work for myself. Found a few weird things while working with the client at a granular level, would be useful experience for me. Appreciate you all taking the time!

@github-project-automation github-project-automation bot moved this from Untriaged to In Progress in Azure SDK for Key Vault Apr 23, 2025
@JonathanCrd JonathanCrd merged commit 303002e into Azure:main Apr 23, 2025
18 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in Azure SDK for Key Vault Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. KeyVault
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants